package shopping.management.onthefly;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;
import shopping.commons.Util;
import shopping.model.Order;
import shopping.model.OrderItem;

import java.util.ArrayList;
import java.util.List;
import shopping.model.LoginInfo;

/**
 * Created by IntelliJ IDEA.
 * User: mazhao
 * Date: 2010/11/04
 * Time: 22:47:56
 * To change this template use File | Settings | File Templates.
 */
@org.apache.struts2.convention.annotation.Namespace("/management/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value="shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
    @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
    @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class OrderAction extends BaseAction {
    // =========================================
    // properties for data transfer
    // =========================================

    private int orderId;
    private List<OrderItem> items = new ArrayList<OrderItem>();
    private int formerStatus;
    private int status;

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public List<OrderItem> getItems() {
        return items;
    }

    public int getFormerStatus() {
        return formerStatus;
    }

    public void setFormerStatus(int formerStatus) {
        this.formerStatus = formerStatus;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    // ========================================
    // properties for ajax error
    // ========================================
    private int retValue;
    private String retMsg;

    public String getRetMsg() {
        return retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }

    public int getRetValue() {
        return retValue;
    }

    public void setRetValue(int retValue) {
        this.retValue = retValue;
    }
    // ========================================
    // action methods
    // ========================================

    @Action(value="/management/onthefly/listorderitems",
        results={
            @Result(name="success", type="json")
        }
    )
    public String listOrderItems() {
        log.debug("enter /management/onthefly/listorderitems action");
        log.debug("the specified order id is " + orderId);
        Order o = new Order();
        o.setId(orderId);
        this.items = o.getOrderItems();
        if(log.isDebugEnabled()) {
            for(OrderItem i : items) {
                log.debug(i);
            }
        }
        this.retValue = 0;
        return SUCCESS;
    }

    @Action(value="/management/onthefly/changestatus",
        results={
            @Result(name="success", type="json")
        }
    )
    public String changeOrderStatus() {
        log.debug("enter /management/onthefly/changestatus action");
        log.debug("chagne status from " + formerStatus + " to " + status);

        Order o = new Order();
        o.setId(orderId);

//        LoginInfo login = (LoginInfo) ActionContext.getContext().getSession().get(LoginInfo.SK_LOGIN_INFO);
        LoginInfo login = Util.Security.retriveUserFromSession();

        log.debug("user login information " + login);
        String user = login.getUsername();
        if(Order.ORDER_STATUS_SENT == status && Order.ORDER_STATUS_NEW == formerStatus) {
            log.debug("new to sent");
            o.newToSent(user);
        } else if(Order.ORDER_STATUS_SENT == status && Order.ORDER_STATUS_PENDING == formerStatus) {
            log.debug("pending to sent");
            o.pendingToSent(user);
        } else if(Order.ORDER_STATUS_PENDING == status && Order.ORDER_STATUS_NEW == formerStatus) {
            log.debug("new to pending");
            o.newToPending(user);
        }

        this.setRetValue(0);
        this.setRetMsg("update status successfully");
        return SUCCESS;
    }
}
